/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.dal.mongo.mongo;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;

import cn.icanci.loopstack.damc.admin.common.utils.IdHolder;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.BaseDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.BaseDO;
import cn.icanci.loopstack.damc.admin.dal.utils.EnvUtils;

/**
 * @author icanci
 * @since 1.0 Created in 2023/08/19 18:00
 */
public abstract class AbstractBaseDAO<T extends BaseDO> extends MongoPageHelper implements BaseDAO<T>, InitializingBean {

    protected String DEFAULT_ENV;

    @Override
    public void insert(T t) {
        // 处理插入数据
        doInsert(t);
    }

    @Override
    public void update(T t) {
        t.setUpdateTime(new Date());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        DEFAULT_ENV = EnvUtils.getEnv();
    }

    public void batchInsert(List<T> list) {
        // 处理插入数据
        list.forEach(this::doInsert);
    }

    protected void doInsert(T t) {
        t.setId(null);
        t.setDeleted(false);
        t.setCreateTime(new Date());
        t.setUpdateTime(new Date());
        t.setEnv(DEFAULT_ENV);
        t.setUuid(IdHolder.generateNoBySnowFlakeDefaultPrefix());
    }
}
